<?php
namespace app\admin\validate;

use think\Validate;

class Article extends Validate
{
    protected $rule = [
        'cid'   => 'require',
        'title' => 'require|unique:article',
        'introduction' => 'require',
        'down_link' => 'is_url:1',
        'thumb' => 'require',
        'sort'  => 'require|number'
    ];

    protected $message = [
        'cid.require'   => '请选择所属栏目',
        'title.require' => '请输入标题',
        'title.unique' => '文章已存在',
        'introduction.require' => '请输入文章简介',
        'thumb.require' => '请上传缩略图',
        'sort.require'  => '请输入排序',
        'sort.number'   => '排序只能填写数字'
    ];
    
    
    protected function is_url($value){
        if($value){
            $exp = "/^(https?:\/\/)?(((www\.)?[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)?\.([a-zA-Z]+))|(([0-1]?[0-9]?[0-9]|2[0-5][0-5])\.([0-1]?[0-9]?[0-9]|2[0-5][0-5])\.([0-1]?[0-9]?[0-9]|2[0-5][0-5])\.([0-1]?[0-9]?[0-9]|2[0-5][0-5]))(\:\d{0,4})?)(\/[\w- .\/?%&=]*)?$/i";  
            if(preg_match($exp,$value)){  
                return true;  
            }else{  
                return '下载链接格式不正确';  
            }  
        }
    }
    
}